package com.example.pkmedical.APP;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import com.example.pkmedical.Utils.LanguageUtil;
import com.raizlabs.android.dbflow.config.FlowConfig;
import com.raizlabs.android.dbflow.config.FlowManager;

import org.litepal.LitePal;

import java.util.Locale;

/**
 * created by dan
 */
public class MyApp extends Application {
    private static MyApp mInstance;
    private static Context context;
    private int minTemp, maxTemp, lgType;
    private SharedPreferences sharedPreferences;
    @Override
    public void onCreate() {
        super.onCreate();
        FlowManager.init(new FlowConfig.Builder(this).build());
        MyApp.context = getApplicationContext();
        //本地语言管理
        Locale locale = LanguageUtil.getAppLocale(this);
        LanguageUtil.changeAppLanguage(this, locale);
        //注册Activity生命周期监听回调
        registerActivityLifecycleCallbacks(callbacks);
        LitePal.initialize(this);
        //获取到SQLiteDatabase的实例，创建数据库表
        SQLiteDatabase db = LitePal.getDatabase();
    }
    ActivityLifecycleCallbacks callbacks = new ActivityLifecycleCallbacks() {
        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
            if (!LanguageUtil.isSameWithSetting(activity)) {
                LanguageUtil.changeAppLanguage(activity,
                        LanguageUtil.getAppLocale(activity));
            }

        }

        @Override
        public void onActivityStarted(Activity activity) {

        }

        @Override
        public void onActivityResumed(Activity activity) {

        }

        @Override
        public void onActivityPaused(Activity activity) {

        }

        @Override
        public void onActivityStopped(Activity activity) {

        }

        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

        }

        @Override
        public void onActivityDestroyed(Activity activity) {

        }

        //其他生命周期重载方法。
    };

    public static Context getAppContext() {
        return MyApp.context;
    }


    /**
     * 获取最小温度
     * @return
     */
    public int getMinTemp() {

        sharedPreferences = getSharedPreferences("user",
                Activity.MODE_PRIVATE);
        String minStr = sharedPreferences.getString("minStr", "");
        String maxStr = sharedPreferences.getString("maxStr", "");
        if (minStr != null && !minStr.equals("")) {
            minTemp = Integer.parseInt(minStr);
            maxTemp = Integer.parseInt(maxStr);
        } else {
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString("minStr", "0");
            editor.putString("maxStr", "70");
            editor.commit();
            minTemp = 0;
            maxTemp = 30;
        }
        return minTemp;
    }


    public void setMinTemp(int minTemp) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("minStr", minTemp + "");
        editor.commit();
        this.minTemp = minTemp;
    }
    /**
     * 获取最大温度
     * @return
     */
    public int getMaxTemp() {
        sharedPreferences = getSharedPreferences("user",
                Activity.MODE_PRIVATE);
        String minStr = sharedPreferences.getString("minStr", "");
        String maxStr = sharedPreferences.getString("maxStr", "");
        if (minStr != null && !minStr.equals("")) {
            minTemp = Integer.parseInt(minStr);
            maxTemp = Integer.parseInt(maxStr);
        } else {
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString("minStr", "0");
            editor.putString("maxStr", "70");
            editor.commit();
            minTemp = 0;
            maxTemp = 30;
        }
        return maxTemp;
    }

    public void setMaxTemp(int maxTemp) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("maxStr", maxTemp + "");
        editor.commit();
        this.maxTemp = maxTemp;
    }

    /**
     * 获取语言类型
     * @return
     */
    public int getLgType() {
        sharedPreferences = getSharedPreferences("user",
                Activity.MODE_PRIVATE);
        String lgStr = sharedPreferences.getString("lgStr", "");
        if (lgStr != null && !lgStr.equals("")) {
            lgType = Integer.parseInt(lgStr);
        } else {
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString("lgStr", "0");
            editor.commit();
            lgType = 0;
        }
        return lgType;
    }

    public void setLgType(int lgType) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("lgStr", lgType + "");
        editor.commit();
        this.lgType = lgType;
    }
}
